package com.cq.hd.rabbit.config;

import com.cq.hd.common.constant.RabbitMqConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: rabbitmq的 交换机,路由,队列的定义和绑定
 * @author: mYunYu
 * 2021/8/3 18:18
 * @version: v1.0
 */
@Component
public class RabbitMqConfig {

    @Bean(RabbitMqConstant.BUSINESS_ENTER_EXCHANGE_DIRECT)
    public Exchange businessEnterExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.BUSINESS_ENTER_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.BUSINESS_ENTER_QUEUE)
    public Queue businessEnterQueue() {
        return new Queue(RabbitMqConstant.BUSINESS_ENTER_QUEUE);
    }

    @Bean
    public Binding businessEnterRoutingKey(@Qualifier(RabbitMqConstant.BUSINESS_ENTER_QUEUE) Queue queue,
                                           @Qualifier(RabbitMqConstant.BUSINESS_ENTER_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.BUSINESS_ENTER_ROUTING_KEY).noargs();
    }

    @Bean(RabbitMqConstant.MERCHANT_ENTER_EXCHANGE_DIRECT)
    public Exchange merchantEnterExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.MERCHANT_ENTER_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.MERCHANT_ENTER_QUEUE)
    public Queue merchantEnterQueue() {
        return new Queue(RabbitMqConstant.MERCHANT_ENTER_QUEUE);
    }

    @Bean
    public Binding merchantEnterRoutingKey(@Qualifier(RabbitMqConstant.MERCHANT_ENTER_QUEUE) Queue queue,
                                           @Qualifier(RabbitMqConstant.MERCHANT_ENTER_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.MERCHANT_ENTER_ROUTING_KEY).noargs();
    }

    @Bean(RabbitMqConstant.USER_LOGIN_RECORD_EXCHANGE_DIRECT)
    public Exchange userLoginRecordExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.USER_LOGIN_RECORD_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.USER_LOGIN_RECORD_QUEUE)
    public Queue userLoginRecordQueue() {
        return new Queue(RabbitMqConstant.USER_LOGIN_RECORD_QUEUE);
    }

    @Bean
    public Binding userLoginRecordRoutingKey(@Qualifier(RabbitMqConstant.USER_LOGIN_RECORD_QUEUE) Queue queue,
                                             @Qualifier(RabbitMqConstant.USER_LOGIN_RECORD_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.USER_LOGIN_RECORD_ROUTING_KEY).noargs();
    }

    @Bean(RabbitMqConstant.MERCHANT_LOGIN_RECORD_EXCHANGE_DIRECT)
    public Exchange merchantLoginRecordExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.MERCHANT_LOGIN_RECORD_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.MERCHANT_LOGIN_RECORD_QUEUE)
    public Queue merchantLoginRecordQueue() {
        return new Queue(RabbitMqConstant.MERCHANT_LOGIN_RECORD_QUEUE);
    }

    @Bean
    public Binding merchantLoginRecordRoutingKey(@Qualifier(RabbitMqConstant.MERCHANT_LOGIN_RECORD_QUEUE) Queue queue,
                                                 @Qualifier(RabbitMqConstant.MERCHANT_LOGIN_RECORD_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.MERCHANT_LOGIN_RECORD_ROUTING_KEY).noargs();
    }

    @Bean(RabbitMqConstant.DL_ORDER_EXCHANGE_DIRECT)
    public Exchange dlOrderExchangeDirect() {
        Map<String, Object> args = new HashMap<>();
        //自定义交换机的类型
        args.put(RabbitMqConstant.X_DELAYED_TYPE_KEY, RabbitMqConstant.X_DELAYED_TYPE_VALUE);
        return new CustomExchange(RabbitMqConstant.DL_ORDER_EXCHANGE_DIRECT, RabbitMqConstant.X_DELAYED_MESSAGE, true, false, args);
    }

    @Bean(RabbitMqConstant.DL_ORDER_QUEUE)
    public Queue dlOrderQueue() {
        return new Queue(RabbitMqConstant.DL_ORDER_QUEUE);
    }

    @Bean
    public Binding dlOrderRoutingKey(@Qualifier(RabbitMqConstant.DL_ORDER_QUEUE) Queue queue,
                                     @Qualifier(RabbitMqConstant.DL_ORDER_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.DL_ORDER_ROUTING_KEY).noargs();
    }

    @Bean(RabbitMqConstant.ORDER_REFUND_EXCHANGE_DIRECT)
    public Exchange orderRefundExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.ORDER_REFUND_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.ORDER_REFUND_QUEUE)
    public Queue orderRefundQueue() {
        return new Queue(RabbitMqConstant.ORDER_REFUND_QUEUE);
    }

    @Bean
    public Binding orderRefundRoutingKey(@Qualifier(RabbitMqConstant.ORDER_REFUND_QUEUE) Queue queue,
                                         @Qualifier(RabbitMqConstant.ORDER_REFUND_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.ORDER_REFUND_ROUTING_KEY).noargs();
    }

    @Bean(RabbitMqConstant.ORDER_VERIFY_EXCHANGE_DIRECT)
    public Exchange orderVerifyExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.ORDER_VERIFY_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.ORDER_VERIFY_QUEUE)
    public Queue orderVerifyQueue() {
        return new Queue(RabbitMqConstant.ORDER_VERIFY_QUEUE);
    }

    @Bean
    public Binding orderVerifyRoutingKey(@Qualifier(RabbitMqConstant.ORDER_VERIFY_QUEUE) Queue queue,
                                         @Qualifier(RabbitMqConstant.ORDER_VERIFY_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.ORDER_VERIFY_ROUTING_KEY).noargs();
    }


    @Bean(RabbitMqConstant.NOTIFY_MSG_EXCHANGE_DIRECT)
    public Exchange notifyMsgExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.NOTIFY_MSG_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.NOTIFY_MSG_QUEUE)
    public Queue notifyMsgQueue() {
        return new Queue(RabbitMqConstant.NOTIFY_MSG_QUEUE);
    }

    @Bean
    public Binding notifyMsgRoutingKey(@Qualifier(RabbitMqConstant.NOTIFY_MSG_QUEUE) Queue queue,
                                       @Qualifier(RabbitMqConstant.NOTIFY_MSG_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.NOTIFY_MSG_ROUTING_KEY).noargs();
    }


    @Bean(RabbitMqConstant.ACTIVE_BROWSE_EXCHANGE_DIRECT)
    public Exchange activeBrowseExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.ACTIVE_BROWSE_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.ACTIVE_BROWSE_QUEUE)
    public Queue activeBrowseQueue() {
        return new Queue(RabbitMqConstant.ACTIVE_BROWSE_QUEUE);
    }

    @Bean
    public Binding activeBrowseRoutingKey(@Qualifier(RabbitMqConstant.ACTIVE_BROWSE_QUEUE) Queue queue,
                                       @Qualifier(RabbitMqConstant.ACTIVE_BROWSE_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.ACTIVE_BROWSE_ROUTING_KEY).noargs();
    }

    @Bean(RabbitMqConstant.WITHDRAWAL_EXCHANGE_DIRECT)
    public Exchange withdrawalExchangeDirect() {
        return ExchangeBuilder.directExchange(RabbitMqConstant.WITHDRAWAL_EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(RabbitMqConstant.WITHDRAWAL_QUEUE)
    public Queue withdrawalQueue() {
        return new Queue(RabbitMqConstant.WITHDRAWAL_QUEUE);
    }

    @Bean
    public Binding withdrawalRoutingKey(@Qualifier(RabbitMqConstant.WITHDRAWAL_QUEUE) Queue queue,
                                          @Qualifier(RabbitMqConstant.WITHDRAWAL_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMqConstant.WITHDRAWAL_ROUTING_KEY).noargs();
    }

}
